package com.nf.mapper;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.nf.entity.Account;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Date;
import java.sql.SQLException;
import java.util.List;

/**
 * 访问数据库（业务逻辑不管，html不管，跳转不管）
 * 只管数据库（高内聚）
 */
public class TestDaoImpl implements TestDao {


    private ComboPooledDataSource pool;

    public void setPool(ComboPooledDataSource pool) {
        this.pool = pool;
    }

    public TestDaoImpl(){

        System.out.println("第一步：TestDaoImpl的实例创建了，"+this);
    }

    /*
    public float getFloat() {
        float balance = 0.0f;
        //一大堆JDBC的代码（作业：不能直接创建Connection）
        //要改为从pool获取连接  pool.getConnection()
        //复习：jdbc
        //获取数据库连接，只用了2行代码
        System.out.println( "pool:"+pool  );
        try {
            Connection connection = pool.getConnection();
            PreparedStatement pst = connection.prepareStatement("select balance from account where id=2");
            ResultSet rs = pst.executeQuery();
            if (rs.next()){
                balance = rs.getFloat("balance");
                System.out.println("从数据库获得balance数据:"+balance);
            }
            rs.close();
            pst.close();
            connection.close();

//            long start = System.currentTimeMillis();
//            Connection conn = pool.getConnection();
//
//            long end = System.currentTimeMillis();
//            System.out.println("耗时:"+  ( end-start ));
//            conn.close();
//            //获取了100次连接
//            //但是每次都是从连接池取，不是新创建
//            //获来获去都是那10个
//            for (int i=0;i<100;i++) {
//                long start1 = System.currentTimeMillis();
//                conn = pool.getConnection();
//                long end1 = System.currentTimeMillis();
//                System.out.println("第2次获取连接耗时:" + (end1 - start1)+conn);
//                conn.close();
//            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return balance;
        //return 100.0f;
    }
    */

    @Override
    public Account getAccountById( int accountId ){
        String sql2 = "select balance/11 from account where id=3";//有没有傻逼这样写
        String sql = "select * from account where id=?";
        try {
            Connection conn = pool.getConnection();
            PreparedStatement pst = conn.prepareStatement(sql);
            pst.setInt(1, accountId  );//大名
            ResultSet rs = pst.executeQuery();
            if (rs.next()) {
                Account account = new Account();
                account.setId(rs.getInt("id"));
                account.setUserName(rs.getString("user_name"));
                account.setBalance(rs.getFloat("balance"));
                return account;
            } else {
                rs.close();
                pst.close();
                conn.close();
                return null;
            }
        }catch (Exception e){
            System.out.println("getAccountById访问数据库有异常:"+e.getMessage());
            return null;
        }

    }

    @Override
    public List<Account> getAccountList() {
        return null;
    }

    @Override
    public boolean updateAccount(Account account) {
        return false;
    }

    @Override
    public boolean addAccount(Account account) {
        return false;
    }

    @Override
    public boolean delAccount(Account account) {
        return false;
    }
}
